package cn.zjxiliu.lib_core.utils;

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;

/**
 * Created by jessie on 2018/10/25.
 */

public class Base64Utils {
    private static final char[] chars64 = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
    private static final int[] ints64 = new int[128];

    public static final String encode(byte[] unencoded) {
        ByteArrayOutputStream out = new ByteArrayOutputStream((int)((double)unencoded.length * 1.37D));
        int byteCount = 0;
        int carryOver = 0;

        int lookup;
        for(lookup = 0; lookup < unencoded.length; ++lookup) {
            int bc = byteCount % 3;
            byte b = unencoded[lookup];
            boolean lookup1 = false;
            int var8;
            if(bc == 0) {
                var8 = b >> 2 & 63;
                carryOver = b & 3;
                out.write(chars64[var8]);
            } else if(bc == 1) {
                var8 = carryOver << 4 | b >> 4 & 15;
                carryOver = b & 15;
                out.write(chars64[var8]);
            } else if(bc == 2) {
                var8 = carryOver << 2 | b >> 6 & 3;
                out.write(chars64[var8]);
                var8 = b & 63;
                out.write(chars64[var8]);
                carryOver = 0;
            }

            ++byteCount;
        }

        if(byteCount % 3 == 1) {
            lookup = carryOver << 4 & 240;
            out.write(chars64[lookup]);
            out.write(61);
            out.write(61);
        } else if(byteCount % 3 == 2) {
            lookup = carryOver << 2 & 60;
            out.write(chars64[lookup]);
            out.write(61);
        }

        return out.toString();
    }

    public static final byte[] decode(String encoded) {
        byte[] bytes = encoded.getBytes();
        ByteArrayOutputStream out = new ByteArrayOutputStream((int)((double)bytes.length * 0.67D));
        int byteCount = 0;
        int carryOver = 0;

        for(int i = 0; i < bytes.length; ++i) {
            byte ch = bytes[i];
            if(ch == 61) {
                break;
            }

            int newbits = ints64[ch];
            int bc = byteCount % 4;
            if(bc == 0) {
                carryOver = newbits & 63;
            } else {
                int data;
                if(bc == 1) {
                    data = (carryOver << 2) + (newbits >> 4 & 3);
                    out.write(data);
                    carryOver = newbits & 15;
                } else if(bc == 2) {
                    data = (carryOver << 4) + (newbits >> 2 & 15);
                    out.write(data);
                    carryOver = newbits & 3;
                } else if(bc == 3) {
                    data = (carryOver << 6) + (newbits & 63);
                    out.write(data);
                    carryOver = 0;
                }
            }

            ++byteCount;
        }

        return out.toByteArray();
    }

    public static final String encodeBase64(String srcString, String charset) {
        if(srcString == null) {
            return null;
        } else {
            if(charset == null || "".equals(charset)) {
                charset = "utf-8";
            }

            return encode(srcString.getBytes(Charset.forName(charset)));
        }
    }

    public static final String decodeBase64(String srcString, String charset) {
        if(srcString == null) {
            return null;
        } else {
            if(charset == null || "".equals(charset)) {
                charset = "utf-8";
            }

            return new String(decode(srcString), Charset.forName(charset));
        }
    }
}
